/*
 * $Id: move.c,v 1.2 2005-09-24 14:54:51 bacon Exp $
 */

#include <xp/utl/fsm.h>
#include <xp/bas/sio.h>

int confirm (int cmd, void* arg, const xp_char_t* file)
{
	xp_char_t buf[128];

	if (cmd == XP_FSM_CONFIRM_OVERWRITE) {
		xp_sio_putsn (xp_sio_out, 
			XP_TEXT("are you sure to overwrite "), 
			file, XP_TEXT("? "), XP_NULL);
		xp_sio_flush (xp_sio_out);
		xp_sio_gets (xp_sio_in, buf, xp_countof(buf));

		if (buf[0] == XP_CHAR('y') || buf[0] == XP_CHAR('Y')) return 0;
	}

	return -1;
}

int xp_main (int argc, xp_char_t* argv[])
{
	xp_fsm_t* fsm;
	xp_char_t errbuf[128];

	if (argc != 3) {
		xp_sio_putsn (xp_sio_err, XP_TEXT("usage: "), argv[0], 
			XP_TEXT(" source target\n"), XP_NULL);
		return -1;
	}

	fsm = xp_fsm_open (XP_NULL);
	if (fsm == XP_NULL) {
		xp_sio_puts (xp_sio_err, XP_TEXT("error: cannot open fsm\n"));
		return -1;
	}

	xp_fsm_attach_confirmer (fsm, confirm, fsm);

	if (xp_fsm_move (fsm, argv[1], argv[2]) == -1) {
		xp_fsm_error (fsm, errbuf, xp_countof(errbuf));
		xp_sio_putsn (
			xp_sio_err, XP_TEXT("error: "), 	
			errbuf, XP_TEXT("\n"), XP_NULL);
		xp_fsm_close (fsm);
		return -1;
	}

	xp_fsm_close (fsm);
	return 0;
}
